﻿@inherits BaseComponent
@inject AppService AppService

<CmsCard Class="cl-doc-menu">
    @if (_contents is { Count: > 0 })
    {
        <ul>
            @foreach (var content in _contents)
            {
                <li>
                    @if (content.Children?.Count > 0)
                    {
                        <div>@content.Name</div>
                        <ul>
                            @foreach (var child in content.Children)
                            {
                                <li>
                                    <a href="@GetUrl(child)" class="@(IsActive(child.Slug) ? "active" : "")">@child.Name</a>
                                </li>
                            }
                        </ul>
                    }
                    else
                    {
                        <a href="@GetUrl(content)" class="@(IsActive(content.Slug) ? "active" : "")">@content.Name</a>
                    }
                </li>
            }
        </ul>
    }
</CmsCard>

@code {
    private List<DocItem>? _contents { get; set; }

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();

        _contents = await AppService.GetAllDocItemsAsync();
    }

    private string GetUrl(DocItem item) => $"/doc/{item.Slug}";

    private bool IsActive(string slug)
    {
        var uri = Navigation.Uri;
        if (string.Equals(slug, _contents?.FirstOrDefault()?.Slug, StringComparison.CurrentCultureIgnoreCase))
        {
            return true;
        }

        return uri.EndsWith($"/{slug}");
    }

}